/**
 * @(#) CompensationMediator.as
 *
 * @date		2010. 11. 01
 * @version		2.0
 * @author		Shin, Yunwoo
 * @client		Service Ace 
 * @module		Compensation  (급여)
 * @description	급여조회   - Mediator
 * 				
 */
package com.skt.common_hr.compensation.view
{
	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.common.components.CommonAlert;
	import com.skt.common_hr.common.events.ParamEvent;
	import com.skt.common_hr.common.puremvc.OverViewMediator;
	import com.skt.common_hr.compensation.CompensationFacade;
	import com.skt.common_hr.compensation.events.SelPayDayEvent;
	import com.skt.common_hr.compensation.model.FiscalYearsProxy;
	import com.skt.common_hr.compensation.model.PassWordCheckProxy;
	import com.skt.common_hr.compensation.model.PaySummaryProxy;
	import com.skt.common_hr.compensation.model.PaymentProxy;
	import com.skt.common_hr.compensation.view.components.NoticeView;
	import com.skt.common_hr.compensation.view.components.OverView;
	import com.skt.common_hr.compensation.view.components.PayDetailView;
	import com.skt.common_hr.compensation.view.components.WageGuideNosView;
	import com.skt.common_hr.compensation.view.components.WageGuideView;
	import com.skt.common_hr.compensation.view.components.popGuide;
	import com.skt.common_hr.compensation.view.components.popGuide_nos;
	
	import flash.events.Event;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;
	import flash.net.navigateToURL;
	
	import mx.core.IFlexDisplayObject;
	import mx.managers.PopUpManager;
	
	import org.puremvc.as3.multicore.interfaces.IMediator;
	import org.puremvc.as3.multicore.interfaces.INotification;
	
	public class CompensationMediator extends OverViewMediator implements IMediator
	{
		public static const NAME:String = "CompensationMediator"; 



		/**
		* 생성자
		* 
		* @param
		* @return void
		*/
        public function CompensationMediator( viewComponent:Object )
        {
            super( NAME, viewComponent ); 
        }
 		
		/**
		* View를 초기화 한다.
		*	- 패스워드 체크 View를 실행하고 Event Listener를 등록한다.
		* @param
		* @return void
		*/
 		override protected function initializeView():void
 		{
 			//passWordMediator.execute();
 			//execPasswordCheckView();
 			
			view.addEventListener(OverView.SelectedYear, onSelectedYear);
			view.addEventListener(SelPayDayEvent.SELECTED_PAYDAY, onSelectedDay);
			view.addEventListener(OverView.ON_CLICK_GUIDE, onClickGuide);
			view.PayDetailView.addEventListener(PayDetailView.ChangePayDetailView, onChangePayDetailView);
			view.PayDetailView.addEventListener(PayDetailView.PRINT_DETAIL, onPrintDetail);
			/* 2011.12.21 jootae 추가 */
			view.addEventListener(OverView.CLICK_NOTICE_DG, noticeDetailPopup);
			clearView();
 		}
 		
		/**
		* 패스워드 체크 View를 실행한다.
		* @param
		* @return void
		*/
 		private function execPasswordCheckView():void
 		{
			var passWordMediator:PassWordCheckMediator = facade.retrieveMediator(PassWordCheckMediator.NAME) as PassWordCheckMediator;
			if(!passWordMediator){
				passWordMediator = new PassWordCheckMediator(view.checkView);
				facade.registerMediator(passWordMediator);
			}else{
				passWordMediator.execute();
			}
 		}
 		
		/**
		* 패스워드 체크 View를 실행한다.
		* @param
		* @return void
		*/
 		override protected function clearView():void
 		{
 			//loadPayList();
			//view.vsCompensation.selectedIndex = 1;
 			view.vsCompensation.selectedIndex = 0;
 			
 			view.checkView.priorPass.text = "";
 			view.checkView.newPass.text = "";
 			view.checkView.reconfirmPass.text = "";
 			
 			view.checkView.initNewPass.text = "";
 			view.checkView.initReconfirmPass.text = "";
 			
 			view.checkView.regno1.text = "";
 			view.checkView.regno2.text = "";
 			
 			//trace("clear Compensation");
 		}
 		
 		
 		private function onPrintDetail(e:Event):void
 		{
          	var params:URLVariables = buildPrintParams();
          	params["PAYTITLE"] =  view.PayDetailView.payTitle;
          	params["ENAME"] = view.PayDetailView.payDetail.ENAME;
          	params["PERNR"] = view.PayDetailView.payDetail.PERNR;
          	params["STEXT"] = view.PayDetailView.payDetail.STEXT;
          	params["BANKA"] = view.PayDetailView.payDetail.BANKA;
          	params["BANKN"] = view.PayDetailView.payDetail.BANKN;
          
          	params["PAYSUM_F"] 		= view.PayDetailView.payDetail.PAYSUM_F;
          	params["SUBTSUM_F"] 	= view.PayDetailView.payDetail.SUBTSUM_F;
          	params["RECEIVEDSUM_F"] = view.PayDetailView.payDetail.RECEIVEDSUM_F;
          	params["PRERECEIVED_F"] = view.PayDetailView.payDetail.PRERECEIVED_F;
          	
          	params["COUNT_P"] = view.PayDetailView.payDetail.payDetail.length;
          	for(var i:int=0; i<view.PayDetailView.payDetail.payDetail.length; i++) {
          		params["ITMNM_P"+i] = view.PayDetailView.payDetail.payDetail[i].ITMNM;
          		params["BETRG_P"+i] = view.PayDetailView.payDetail.payDetail[i].BETRG_F;
          	}
          	
          	params["COUNT_S"] = view.PayDetailView.payDetail.subtDetail.length;
          	for(var j:int=0; j<view.PayDetailView.payDetail.subtDetail.length; j++) {
          		params["ITMNM_S"+j] = view.PayDetailView.payDetail.subtDetail[j].ITMNM;
          		params["BETRG_S"+j] = view.PayDetailView.payDetail.subtDetail[j].BETRG_F;
          	}
         	
          	openNavigateToURL("Compensation.jsp", params);
  		}
  		
  		/* 2011.12.21 jootae 추가 */
  		public var oPopup:IFlexDisplayObject = null;
  		public function noticeDetailPopup(e:Event):void
  		{
  			if(oPopup==null) { // 신규 생성
		        oPopup = PopUpManager.createPopUp(view, NoticeView, true);
		        PopUpManager.centerPopUp(oPopup);
	        }
	        else {
	        	PopUpManager.addPopUp(oPopup, view, true);
   	        }
   	        oPopup.y = 70;
			NoticeView(oPopup).dataProvider = view.selObj;	 	
  		}
 		
 		/* 2013.11.20 추가 */
  		public var oPopupRental:IFlexDisplayObject = null;
  		public function rentalListPopup(e:Event):void 
  		{
  			if(oPopupRental==null) { // 신규 생성
		        oPopupRental = PopUpManager.createPopUp(view, NoticeView, true);
		        PopUpManager.centerPopUp(oPopupRental);
	        }
	        else {
	        	PopUpManager.addPopUp(oPopupRental, view, true);
   	        }
   	        oPopupRental.y = 70;
			NoticeView(oPopupRental).dataProvider = view.selObj;	 	
  		}
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Process MVC Messages
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		* 본 Class에서 처리해야할 Noti들을 등록한다.
		* 
		* @param
		* @return void
		*/
		override public function listNotificationInterests():Array 
		{
			var list:Array = basicInterestMessages.concat( [
					CompensationFacade.LOAD_FISCALYEARS_COMPLETE,
					CompensationFacade.LOAD_FISCALYEARS_FAILED,
					CompensationFacade.LOAD_PAYLIST_COMPLETE,
					CompensationFacade.LOAD_PAYLIST_FAILED,
					CompensationFacade.LOAD_PAYDETAIL_COMPLETE,
					CompensationFacade.LOAD_PAYDETAIL_FAILED,
					CompensationFacade.LOAD_PASSWORD_COMPLETE,
					CompensationFacade.VALID_PASSWORD
					]);
			return list;
		}
		

		/**
		* Noti별로 처리한다.
		* 
		* @param
		* @return void
		*/
		override public function handleNotification(note:INotification):void
		{
			super.handleNotification(note);
			
			switch(note.getName()) 
			{
				case CompensationFacade.LOAD_FISCALYEARS_COMPLETE:
					view.cbYear.dataProvider = (note.getBody() as FiscalYearsProxy).yearList;
		 			var summaryProxy:PaySummaryProxy = facade.retrieveProxy(PaySummaryProxy.NAME) as PaySummaryProxy;
		 			summaryProxy.load(Variables.myPernr,view.cbYear.selectedLabel);
					break;
				case CompensationFacade.LOAD_PAYLIST_COMPLETE:
					view.payList = (note.getBody() as PaySummaryProxy).payList;
					view.payTotal = (note.getBody() as PaySummaryProxy).payTotal;
					commonFacade.setStatusMessage("STATUS_LOADED_PAYLIST", [view.cbYear.selectedLabel]);
					
					if(Variables.company == "NOS" && (note.getBody() as PaySummaryProxy).sO_DYN == "Y") {
						view.btnRentList.visible = true;	
					}
					else {
						view.btnRentList.visible = false;
					}
					
					break; 
				case CompensationFacade.LOAD_PAYDETAIL_COMPLETE:
					view.PayDetailView.payTitle =  (note.getBody() as PaymentProxy).payTitle;
					view.PayDetailView.payDetail = (note.getBody() as PaymentProxy).payment;
					
					if((note.getBody() as PaymentProxy).acPopDetail.length > 0) {
						view.PayDetailView.btnDesc.visible = true;	
						view.PayDetailView.btnDesc.includeInLayout = true;
						view.PayDetailView.acPopDesc = (note.getBody() as PaymentProxy).acPopDetail;
					} else {
						view.PayDetailView.btnDesc.visible = false;	
						view.PayDetailView.btnDesc.includeInLayout = false;
					}
					
					commonFacade.setStatusMessage("STATUS_LOADED_PAYDETAIL");
					break;
				case CompensationFacade.LOAD_PAYLIST_FAILED:
					view.payList = (note.getBody() as PaySummaryProxy).payList;
					view.payTotal = (note.getBody() as PaySummaryProxy).payTotal;
					break;
				case CompensationFacade.VALID_PASSWORD:
					if(Variables.company == 'NOS') {
						//viewNotice();	
						viewNoticeNOS();
					}
					
					loadPayList();
					view.vsCompensation.selectedIndex = 1;
					
					break;
				case CompensationFacade.CHECK_PASSWORD_FAILED:
					loadPayList();
					CommonAlert.showMessage(commonFacade.bundle, (note.getBody() as PassWordCheckProxy).retText);
					break;		
				default: 
					//trace(this.instanceID+"에서 처리 안된 Noti가 검출됨 : " + note.getBody());
					break;
			}
		}


		override protected function activatedView():void
		{
			trace("activatedView");
			clearView();
			execPasswordCheckView();
		}



 		private function loadPayList():void
 		{
 			var yearProxy:FiscalYearsProxy = facade.retrieveProxy(FiscalYearsProxy.NAME) as FiscalYearsProxy;
 			yearProxy.load(Variables.myPernr);
 			
 			if(Variables.company=="SAC"){
 				view.noticeList();
 			}
 			
/* 
 			if(Variables.company=="SAC"){ 				
	 			Alert.show(
	 			"[공지사항]\r\r" + 
	 			"안녕하세요?\r" + 
	 			"4월 급여지급시에는 \r" + 
	 			"'2010년 건강보험 연말정산'이 반영되어 \r" + 
	 			"건강보험료 공제금액이 일시적으로 \r" + 
	 			"증가되었습니다.\r" + 
	 			"이에 전사공지사항을 참고하시고 \r" + 
	 			"문의사항이 있는 경우,\r" + 
	 			"각 지원 담당자분들께 확인해 주시기 \r" + 
	 			"바랍니다.  감사합니다.^^"
	 			); 				
 			}
 */
 		}

 		

 		

		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event handler (from View, Internal or....)
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
 		private function onSelectedYear(event:ParamEvent):void
 		{
 			var summaryProxy:PaySummaryProxy = facade.retrieveProxy(PaySummaryProxy.NAME) as PaySummaryProxy;
 			summaryProxy.load(Variables.myPernr, event.param);
 		}
 		
 		private function onSelectedDay(event:SelPayDayEvent):void
 		{
 			view.vsCompensation.selectedIndex = 2;
			var proxy:PaymentProxy = facade.retrieveProxy(PaymentProxy.NAME) as PaymentProxy;
			proxy.load(event.payVO); 			
 		}
 		
 		private function onChangePayDetailView(event:Event):void
 		{
 			view.vsCompensation.selectedIndex = 1;
 		}
		
		private var guideView:IFlexDisplayObject = null;
		private function onClickGuide(e:Event):void
		{
			if(Variables.company == 'NOS'){
				if(guideView==null) {
					guideView = PopUpManager.createPopUp(view.root, WageGuideNosView, true);
					PopUpManager.centerPopUp(guideView);
				}
				else {
					PopUpManager.addPopUp(guideView, view.root, true);
				}	
			} else {
				if(guideView==null) {
					guideView = PopUpManager.createPopUp(view.root, WageGuideView, true);
					PopUpManager.centerPopUp(guideView);
				}
				else {
					PopUpManager.addPopUp(guideView, view.root, true);
				}
			}
		}

		private var pPopGuide:IFlexDisplayObject = null;
		private var pPopGuideNos:IFlexDisplayObject = null;
		private function viewNotice():void
		{
			if(pPopGuide==null) {
				pPopGuide = PopUpManager.createPopUp(view.root, popGuide, true);
				PopUpManager.centerPopUp(pPopGuide);
			}
			else {
				PopUpManager.addPopUp(pPopGuide, view.root, true);
			}
		}
		
		private function viewNoticeNOS():void
		{
			if(pPopGuideNos==null) {
				pPopGuideNos = PopUpManager.createPopUp(view.root, popGuide_nos, true);
				PopUpManager.centerPopUp(pPopGuideNos);
			}
			else {
				PopUpManager.addPopUp(pPopGuideNos, view.root, true);
			}
		}
		
				
		private function buildPrintParams():URLVariables
		{
			var params:URLVariables = new URLVariables;
			return params;
		}
		
		
			//새창 표시
		public function openNavigateToURL(url:String, params:URLVariables):void {	
			var option:String = "toolbar=no,menubar=no" +
					", location=no, directions=no" 	+ 
					", scrollbars=yes, status=no"	+
					", width=300" +
					", height=760";

	//    System.useCodePage = true;

        	var request:URLRequest = new URLRequest(url);
	//		var request:URLRequest = new URLRequest("javascript:window.open('"+url+"',"+option+"');");

        	request.method = URLRequestMethod.POST;
        	request.data  = params;

   			navigateToURL(request, "_blank");
		}
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Utility Functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
        private function get view():OverView
        {
            return viewComponent as OverView;
        }
	}
}